<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>统计图表</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: Arial, Helvetica, sans-serif;
            background-color: #f9f9f9;
        }
        .chart-container {
            width: 80%;
            margin: 50px auto;
            background: #fff;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
            padding: 20px;
            margin-bottom: 20px;
        }
        #project-status-chart, #user-category-chart {
            width: 100%;
            height: 500px;
        }
        .chart-footer {
            display: flex;
            justify-content: space-between;
            margin-top: 20px;
            padding: 0 20px;
        }
        .total-count {
            font-size: 16px;
            color: #333;
            font-weight: bold;
        }
        .chart-title {
            text-align: center;
            font-size: 18px;
            color: #666;
        }
    </style>
</head>
<body>
<!-- 项目状态统计 -->
<div class="chart-container">
    <div id="project-status-chart"></div>
    <div class="chart-footer">
        <div class="total-count" id="total-project-count">总项目数：0</div>
        <div class="chart-title">项目状态分类统计</div>
    </div>
</div>

<!-- 用户类别统计 -->
<div class="chart-container">
    <div id="user-category-chart"></div>
    <div class="chart-footer">
        <div class="total-count" id="total-user-count">总用户数：0</div>
        <div class="chart-title">用户类别统计</div>
    </div>
</div>

<script>
    // 项目状态饼状图
    var projectChartDom = document.getElementById('project-status-chart');
    var projectChart = echarts.init(projectChartDom);

    var projectOption = {
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [
            {
                name: '项目状态',
                type: 'pie',
                radius: '50%',
                data: [],
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };

    projectChart.setOption(projectOption);

    fetch('/status/statistics')
        .then(response => response.json())
        .then(data => {
            var totalCount = 0;
            var chartData = [];
            var colorMap = {
                '未启动': '#e396a3',
                '培训中': '#92c2e5',
                '培训结束': '#afe39f'
            };
            for (var key in data) {
                if (data.hasOwnProperty(key)) {
                    var statusName = key === '0' ? '未启动' : key === '1' ? '培训中' : '培训结束';
                    var count = data[key];
                    totalCount += count;
                    chartData.push({
                        value: count,
                        name: statusName,
                        itemStyle: {
                            color: colorMap[statusName]
                        }
                    });
                }
            }
            document.getElementById('total-project-count').innerText = `总项目数：${totalCount}`;
            projectOption.series[0].data = chartData;
            projectChart.setOption(projectOption, true);
        })
        .catch(error => {
            console.error('获取项目状态统计数据失败：', error);
        });

    window.addEventListener('resize', function () {
        projectChart.resize();
    });

    // 用户类别柱状图
    var userChartDom = document.getElementById('user-category-chart');
    var userChart = echarts.init(userChartDom);

    var userOption = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        xAxis: {
            type: 'category',
            data: []
        },
        yAxis: {
            type: 'value',
            name: '人数'
        },
        series: [
            {
                name: '人数',
                type: 'bar',
                data: [],
                itemStyle: {
                    color: '#60c5c1'
                }
            }
        ]
    };

    userChart.setOption(userOption);

    fetch('/category/statistics')
        .then(response => response.json())
        .then(data => {
            var totalUserCount = 0;
            var categories = [];
            var counts = [];
            for (var key in data) {
                if (data.hasOwnProperty(key)) {
                    categories.push(key);
                    counts.push(data[key]);
                    totalUserCount += data[key];
                }
            }
            document.getElementById('total-user-count').innerText = `总用户数：${totalUserCount}`;
            userOption.xAxis.data = categories;
            userOption.series[0].data = counts;
            userChart.setOption(userOption, true);
        })
        .catch(error => {
            console.error('获取用户类别统计数据失败：', error);
        });

    window.addEventListener('resize', function () {
        userChart.resize();
    });
</script>
</body>
</html>
